home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 127 / PC Guia 127.iso / Software / Utils / GParted Live CD / Bin / gparted-livecd-0.2.2.iso / sbin / rescan-scsi-bus < prev    next >
Encoding:
Text File  |  2004-06-12  |  2.9 KB  |  120 lines

  1. #!/bin/sh
  2. # Skript to rescan SCSI bus, using the
  3. # scsi add-single-device mechanism
  4. # (w) 98/03/19 Kurt Garloff <kurt@garloff.de> (c) GNU GPL
  5.  
  6. # This script is useful for automatically scanning for new
  7. # USB and IEEE1394 devices which will not show up automatically
  8. # if they use a LUN (logical unit number) other than 0.
  9. # To search all LUNS for new devices to enable, run like this:
  10. #
  11. # rescan-scsi-bus -l
  12.  
  13. found=0
  14. rmvd=0
  15.  
  16. # Return hosts. /proc/scsi/HOSTADAPTER/? must exist
  17. findhosts() {
  18.   hosts=
  19.   for name in /proc/scsi/*/?; do
  20.     name=${name#/proc/scsi/}
  21.     if test ! $name = scsi ; then
  22.       hosts="$hosts ${name#*/}"
  23.       echo "Host adapter ${name#*/} (${name%/*}) found."
  24.     fi
  25.   done
  26. }
  27.  
  28. # Test if SCSI device $host $channen $id $lun exists
  29. # Outputs description from /proc/scsi/scsi, returns new
  30. testexist() {
  31.   grepstr="scsi$host Channel: 0$channel Id: 0*$id Lun: 0$lun"
  32.   new=`cat /proc/scsi/scsi | grep -e "$grepstr"`
  33.   if test ! -z "$new" ; then
  34.     cat /proc/scsi/scsi | grep -e "$grepstr"
  35.     cat /proc/scsi/scsi | grep -A2 -e "$grepstr" | tail -n 2
  36.   fi
  37. }
  38.  
  39. # Perform search (scan $host)
  40. dosearch() {
  41.   for channel in $channelsearch; do
  42.     for id in $idsearch; do
  43.       for lun in $lunsearch; do
  44.         new=
  45.     devnr="$host $channel $id $lun"
  46.     echo "Scanning for device $devnr ..."
  47.     printf "OLD: "
  48.     testexist
  49.     if test ! -z "$remove" -a ! -z "$new" ; then
  50.       echo "scsi remove-single-device $devnr" > /proc/scsi/scsi
  51.       echo "scsi add-single-device $devnr" > /proc/scsi/scsi
  52.       printf "\r\x1b[A\x1b[A\x1b[AOLD: "
  53.       testexist
  54.       if test -z "$new"; then
  55.             printf "\rDEL: \r\n\n\n\n"
  56.             rmvd=$(($rmvd+1))
  57.           fi
  58.     fi
  59.     if test -z "$new" ; then
  60.       printf "\rNEW: "
  61.       echo "scsi add-single-device $devnr" > /proc/scsi/scsi
  62.       testexist
  63.       if test -z "$new"; then
  64.             printf "\r\x1b[A"
  65.           else
  66.             found=$(($found+1))
  67.           fi
  68.     fi
  69.       done
  70.     done
  71.   done
  72. }
  73.       
  74.   
  75. # main
  76. if test @$1 = @--help -o @$1 = @-h ; then
  77.   echo "Usage: rescan-scsi-bus.sh [-l] [-w] [-c] [host [host ...]]"
  78.   echo " -l activates scanning for LUNs 0 .. 7 [default: 0]"
  79.   echo " -w enables scanning for device IDs 0 .. 15 [def.: 0 .. 7]"
  80.   echo " -r enables removing of devices        [default: disabled]"
  81.   echo " -c enables scanning of channels 0 1   [default: 0]"
  82.   echo " If hosts are given, only these are scanned [default: all]"
  83.   exit 0
  84. fi
  85.  
  86. # defaults
  87. lunsearch="0"
  88. idsearch="0 1 2 3 4 5 6 7"
  89. channelsearch="0"
  90. remove=""
  91.  
  92. # Scan options
  93. opt="$1"
  94. while test ! -z "$opt" -a -z "${opt##-*}"; do
  95.   opt=${opt#-}
  96.   case "$opt" in
  97.     l) lunsearch="0 1 2 3 4 5 6 7" ;;
  98.     w) idsearch="0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15" ;;
  99.     c) channelsearch="0 1" ;;
  100.     r) remove=1 ;;
  101.     *) echo "Unknown option -$opt !" ;;
  102.   esac
  103.   shift
  104.   opt="$1"
  105. done    
  106.  
  107. # Hosts given ?
  108. if test @$1 = @; then
  109.   findhosts
  110. else
  111.   hosts=$*
  112. fi
  113.  
  114. for host in $hosts; do
  115.   dosearch
  116. done
  117. echo "$found new device(s) found.               "
  118. echo "$rmvd device(s) removed.                 "
  119.  
  120.